package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dkd.common.exception.ServiceException;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.domain.Vo.VendingMachineVo;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.callback.CallbackHandler;

/**
 * 设备管理Service业务层处理
 * 
 * @author zzg
 * @date 2025-02-27
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private NodeMapper nodeMapper;
    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    @Override
    public VendingMachineVo selectVendingMachineVoById(Long id) {
        return vendingMachineMapper.selectVendingMachineVoById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    @Override
    public List<VendingMachineVo> selectVendingMachineVoList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineVoList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //给设备表新增一条数据
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        //获取点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        //获取设备类型
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setVmStatus(0);
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //给货道表新增n条数据
        List<Channel> channelList = new ArrayList<>();//货道集合
        Integer row = Math.toIntExact(vmType.getVmRow());//行
        Integer col = Math.toIntExact(vmType.getVmCol());//列
        Channel channel = new Channel();
        channel.setVmId(vendingMachine.getId());
        channel.setInnerCode(vendingMachine.getInnerCode());
        channel.setMaxCapacity(vmType.getChannelMaxCapacity());
        Date nowDate = DateUtils.getNowDate();
        channel.setCreateTime(nowDate);
        channel.setUpdateTime(nowDate);
        for (Integer i = 1; i <= row; i++) {
            for (Integer j = 1; j <= col; j++) {
                Channel c = new Channel();
                BeanUtils.copyProperties(channel, c);
                c.setChannelCode(i + "_" + j);
                channelList.add(c);
            }
        }
        channelMapper.insertChannelList(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {   //修改设备表
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        int result = vendingMachineMapper.updateVendingMachine(vendingMachine);
        return result;
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 修改设备策略
     * @param vendingMachine
     * @return
     */
    public int updatePolicy(VendingMachine vendingMachine) {
        return vendingMachineMapper.updatePolicyById(vendingMachine);
    }

}
